Skip to content

refactor(diff): share Crossplane's Composition selection order - #607

Draft
jcogilvie wants to merge 54 commits into
crossplane:mainfrom
jcogilvie:jco-shared-composition-selection
Draft

jcogilvie wants to merge 54 commits into
crossplane:mainfrom
jcogilvie:jco-shared-composition-selection

Conversation

@jcogilvie

@jcogilvie jcogilvie commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Description of your changes

Note

This builds on #601 and includes its commits until #601 merges. Only the last 9 commits are new: compare view. I'll rebase onto main once #601 merges.

The CLI picks a Composition for a composite in two places: FindMatchingComposition in the diff client, for composites in the cluster (xr simulate, and the diff engine), and selectComposition, for project simulate --resource-file inputs against the project's Compositions (#601). Both have to follow Crossplane's selection order, and they had drifted. The cluster path ignored the XRD's enforcedCompositionRef and defaultCompositionRef, so xr simulate could preview a different Composition than Crossplane would select.

This adds SelectComposition (internal/diff/client/crossplane/composition_select.go). It owns the order and the field reads: the XRD's enforced ref, then the composite's compositionRef, its compositionSelector, the XRD's default ref, then the only Composition of its type. Each caller supplies a CompositionLookup for its own source:

  • Cluster: looks a named Composition up through its revisions, lists only label-matching Compositions for a selector (it never lists every Composition there), and keeps its existing error wrapping.
  • Project: works over the project's built Compositions with feat(simulate): render resource files against new Compositions #601's error messages.

Behaviour changes for xr simulate:

  • It applies the XRD's enforcedCompositionRef and defaultCompositionRef, resolving their revisions as it does for compositionRef. A nested composite's enforced Composition also overrides its compositionRef.
  • A compositionSelector that is present but empty skips the default ref, as in Crossplane. With several Compositions of the type that's an error, since Crossplane would pick one at random. A null selector still uses the default.
  • A compositionRef.name or selector label that isn't a string is now an error ("cannot read compositionRef/compositionSelector for ") instead of being skipped or dropped. The project path and the diff engine already rejected these.
  • A nested composite's compositionRef is read at its XRD's v1 or v2 path, not inferred from the composite's apiVersion.

Otherwise selections and messages are unchanged. NewCompDiffProcessor now takes a DefinitionClient.

Tests:

  • TestSelectComposition: a recording fake lookup covers every precedence step, empty and null selectors, and malformed fields, on v1 and v2 XRDs.
  • TestSelectionMatchesCluster: the same inputs run through both lookups and must pick the same Composition.
  • FindMatchingComposition and runCompositionFor gain rows for enforced and default refs, malformed fields and legacy nested composites.

go test ./..., go test -tags integration -p 1 ./... and golangci-lint run ./... pass locally.

I have:

  • Read and followed Crossplane's contribution process.
  • Read and followed Crossplane's AI policy and confirmed a human stands behind this PR.
  • Run ./nix.sh flake check to ensure this PR is ready for review.
  • Added or updated unit tests.
  • Linked a PR or a docs tracking issue to document this change.
  • Added backport release-x.y labels to auto-backport this PR.

Need help with this checklist? See the cheat sheet.

🤖 Generated with Claude Code

jcogilvie and others added 30 commits November 25, 2026 09:02
WithSuppliedComposites adds caller-supplied composites, keyed by
Composition name, to the discovered or scoped set. A supplied composite
with a discovered one's identity replaces it, so it renders once.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Pick each input's Composition the way Crossplane does: compositionRef,
compositionSelector, the XRD's defaultCompositionRef, then the sole
Composition of the composite type.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Errors about project simulate's --resource-file inputs say so; xr and
composition simulate keep saying -f.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
An XR of a v1 XRD and a claim read compositionRef and compositionSelector
under spec only; a v2 XR prefers spec.crossplane, as the engine does.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Run checks the inputs against the cluster's XRDs once the clients
initialize, assigns each to a project Composition and hands them to the
Composition diff processor.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Project simulate without --resource-file appends a note for each created
Composition no composite would adopt, pointing at --resource-file. -o diff
prints it to stderr.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
--namespace now defaults the namespace of inputs that set none, duplicate
input identities fail, and an XRD's enforcedCompositionRef wins as in
Crossplane. The ambiguous-selector test now exercises the selector.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
A --resource-file composite the cluster lacks no longer counts as
re-pointing, and is not deferred for a Manual policy since a new
composite takes the latest revision. Supplied composites render even
when impact analysis is skipped, and a live composite supplied under
another Composition renders once, there. Impacts mark them Supplied.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Skipped-analysis messages now confine their claim to the cluster's
composites and name the supplied composites that were rendered, and
the diff renderer lists those composites rather than only the filtered
ones. Composite lines and plan headers carry (from --resource-file).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Skip the duplicate check for inputs with no name, read the XRD's
enforcedCompositionRef in a test, and test the --namespace wiring
through inputNamespace.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Under a skipped impact analysis with --resource-file composites, -o diff
now prints the filtered-composites line before the rendered list, and the
skip message keeps its --analyze-on remedy. ImpactAnalysisSkipped docs
note the supplied exception.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
A --resource-file composite not discovered under the run is now looked
up in the cluster, so a live one that --resource, --namespace or a
Composition outside the run leaves out keeps its update policy and
counts as re-pointing. A lookup error is reported as that composite's
error. A net-new claim's backing XR is net-new too, so a Manual claim
renders with the predicted revision. Composites with only a
generateName are never matched or looked up.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
DiffProcessor stays within interfacebloat's limit; the composition
processor asserts ClusterCompositeGetter where it needs the lookup.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Drops the optional ClusterCompositeGetter interface, its type assertion
and the unreachable fallback branch.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Replaces suppliedIdentity and the parallel diff-key maps (lookupErr,
netNewKeys' string keys) with result.CompositeKey, so liveness, merge and
net-new lookups share one key that internal/simulate can reuse.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Export XRTypeFromXRD and NestedCrossplaneValue from the diff Crossplane
client and use them in assign.go, use labels.SelectorFromSet for the
selector match, key the duplicate check by result.CompositeKey, and use
the testutils Composition builder. A claim now composes its XRD's first
referenceable version, and an XRD without group or kind fails.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
An empty selector value again matches a Composition lacking the label.
crossplaneField drops its unused type name and names the v2 API version.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
jcogilvie and others added 6 commits November 25, 2026 09:08
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Reporting the unresolved claims under a skip, from main, pushed processSingleComposition over the gocognit limit. The check right after the skip already returns when no supplied composite is left to render.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
A --resource-file XR rarely names the compositionRevisionRef Crossplane
wrote, so a Manual one replacing a live pinned XR was classified as
re-pointing while it rendered as pinned. Read its pin via pinnedRevision,
as for claims, and restore the fixtures that model this.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
…nalysis

Keep the unresolved entries when the analysis is skipped, so a supplied
claim whose pin could not be read gets main's error instead of a render.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
… composite

A --resource-file XR that omits compositionUpdatePolicy over a live Manual
XR was classified Automatic and re-pointed, while render inherits Manual
from the cluster and stays pinned. Read both the policy and the ref from
the effective composite, resolved once; a policy the file sets still wins,
as in render.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
The effective-composite wording belongs to a live supplied XR, whose pin is
now read in classifyXR; pinnedRevision serves claims alone, so it reports
the backing XR of the claim, as on main.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.27187% with 20 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.75%. Comparing base (df6937b) to head (9045358).
⚠️ Report is 7 commits behind head on main.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

jcogilvie and others added 17 commits November 25, 2026 10:41
…ite throughout

Match the selector against the effective composite, so a file naming no
policy over a live Manual XR ignores its selector as render does. Read a
live supplied claim's policy and pin from its backing XR, resolved once
and with main's wording. Resolve a live supplied composite only when the
file leaves its pin open: no policy, or Manual with no ref.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
…ifyXR

Keeps classifyXR under the cognitive-complexity limit.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
…mposition refs

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
…ders

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
…ion client builder

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
…ared helpers

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
One generic function owns the order (the XRD's enforcedCompositionRef,
compositionRef, compositionSelector, the XRD's defaultCompositionRef,
then the type) and the field reads; callers back each step with a
CompositionLookup over their own Compositions.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
FindMatchingComposition now applies the XRD's enforcedCompositionRef and
defaultCompositionRef, as Crossplane does, resolving their revisions as
for a compositionRef. project simulate's selection is unchanged. A shared
table checks both paths pick the same Composition.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Crossplane applies defaultCompositionRef only to a composite with no
compositionSelector at all. Also pass Malformed's field separately so
it can't index an empty path.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
runCompositionFor took the run's Composition named in compositionRef
before consulting the XRD, bypassing enforcedCompositionRef.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
@jcogilvie
jcogilvie force-pushed the jco-shared-composition-selection branch from 19bd296 to a715711 Compare November 25, 2026 17:44
…uilder

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant